package leetcode.code003.LongestSubstringWithoutRepeatingCharacters;

/**
 * 双针 数组记录
 *
 * @author Max
 * @date 2021年8月17日
 * @description
 */
public class Solution2 extends LongestSubstringWithoutRepeatingCharacters {

	@Override
	public int lengthOfLongestSubstring(String s) {
		char[] strings = s.toCharArray();
		int len = strings.length;
		if (len == 0)
			return 0;
		int r = 0;
		int l = 0;
//		Set<Character> set = new HashSet<Character>();
		boolean[] exists = new boolean[128];
		int max = 0;
		int count = 0;
		while (r < len) {
			if (exists[strings[r]]) {
				max = max > count ? max : count;
				while (strings[l] != strings[r]) {
					exists[strings[l]] = false;
					l++;
					count--;
				}
				l++;
				r++;
			} else {
				exists[strings[r]] = true;
				r++;
				count++;
			}
		}
		return max > count ? max : count;
	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}
}
